package com.atguigu.Serivce.Impl;

import com.atguigu.Controller.ShoppingCartController;
import com.atguigu.Serivce.SetmealDishService;
import com.atguigu.Serivce.ShoppingCartService;
import com.atguigu.Utils.BaseContext;
import com.atguigu.Utils.R;
import com.atguigu.mapper.DishMapper;
import com.atguigu.mapper.SetmealMapper;
import com.atguigu.mapper.ShoppingCartMapper;
import com.atguigu.pojo.Dish;
import com.atguigu.pojo.Setmeal;
import com.atguigu.pojo.ShoppingCart;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public R add(ShoppingCart cart) {

        //获得用户id
        Long currentId = BaseContext.getCurrentId();
        cart.setUserId(currentId);

        //查询添加到购物车的究竟是菜品还是套餐
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, currentId);
        if (cart.getDishId() != null) {
            //添加的是菜品
            wrapper.eq(ShoppingCart::getDishId, cart.getDishId());
        }else {
            //添加的是套餐
            wrapper.eq(ShoppingCart::getSetmealId, cart.getSetmealId());
        }

        //查询当前菜品或套餐是否在购物车中,
        //购物车中有该菜品或套餐，则直接让该菜品或套餐数量+1
        //购物车中没有该菜品或套餐，则加入购物车
        ShoppingCart shoppingCart = shoppingCartMapper.selectOne(wrapper);
        if (shoppingCart != null) {
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.updateById(shoppingCart);
        }else {
            cart.setNumber(1);
            cart.setCreateTime(new Date());
            shoppingCartMapper.insert(cart);
            shoppingCart=cart;
        }


        return R.success(shoppingCart);
    }

    @Override
    public R listCart() {
        log.info("展示购物车");
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        wrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(wrapper);
        return R.success(shoppingCarts);
    }

    @Override
    public R sub(ShoppingCart cart) {
        //获得用户id
        cart.setUserId(BaseContext.getCurrentId());
        //查询当前数量，数量小于等于1直接删除，数量>1，则减1
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        queryWrapper.eq(ShoppingCart::getSetmealId, cart.getSetmealId()).or();
        queryWrapper.eq(ShoppingCart::getDishId, cart.getDishId());

        ShoppingCart shoppingCart = shoppingCartMapper.selectOne(queryWrapper);
        if (shoppingCart.getNumber()<=1) {
            shoppingCartMapper.delete(queryWrapper);
        }else {
            shoppingCart.setNumber(shoppingCart.getNumber()-1);
            shoppingCartMapper.updateById(shoppingCart);
        }

        return R.success(shoppingCart);
    }

    @Override
    public R cleanCart() {//清空购物车
        Long currentId = BaseContext.getCurrentId();//当前用户id
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, currentId);
        shoppingCartMapper.delete(wrapper);

        return R.success("清空购物车");
    }


}
